from flask import Blueprint
from flask import request
from flask import jsonify
import json
import datetime
import random
from index import db

bp_coupon = Blueprint('coupon',__name__)

@bp_coupon.route('/add',methods=['POST','GET'])
def add():
    if request.method == "POST":
        # 获取vue发过来的参加
        data = json.loads(request.data)
        print(data)
        print(12345678)
        code = datetime.datetime.strftime(datetime.datetime.now(), '%Y%M%D%H%M%S') + str(random.randint(10000, 99999))
        total = int(data['total'])
        # 用参数构造sql
        sql = "insert into coupon(code,man,jian,stime,etime,total) values('%s',%d,%d,'%s','%s',%d)" % (
        code, int(data['man']), int(data['jian']), data['stime'], data['etime'], total)
        # 调用database里面的添加insert方法进行添加

        db.insert(sql)
        return jsonify({'code': 200})
    else:
        return '获取'
